package com.stars.empoly.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.stars.empoly.adapter.AddressListAdapter
import com.stars.empoly.base.MBRActivity
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.databinding.ActivityAddressListBinding
import com.stars.empoly.model.AddressList
import com.stars.empoly.vmodel.AddressListVModel
import com.trailer.base.net.IResp
import com.trailer.base.net.PageData

/**
 *on 2022/9/24
 */
class AddressListActivity : MBRActivity<AddressListVModel, AddressListAdapter, ActivityAddressListBinding>(){

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
         vm.type=intent.extras?.getString("type","-1")
        vm.ll=binding.ll
        vm.position.observe(this){

            adapter().removeAt(it)
        }

        vm.defautIndex.observe(this){
            var list=adapter().data
            for ((index, value) in list.withIndex()) {
                if(index==it){
                    value.isDefault=1
                    adapter().notifyItemChanged(index)
                }else{
                    if(value.isDefault==1){
                        value.isDefault=0
                        adapter().notifyItemChanged(index)
                    }
                }
            }


        }
    }

    override fun onListReceive(res: IResp<*>) {
        super.onListReceive(res)
        if(null!=res){
            var list=res.data() as PageData<AddressList>
            if(null!=list.content&& list.content.isNotEmpty()){

                if(list.content[0].isDefault==0){
                    //设置默认
                    vm.toSetDefault(list.content[0].id,0,list.content[0].isDefault)
                }
            }
        }
    }





    override fun vmClass(): Class<AddressListVModel> {
        return AddressListVModel::class.java
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_TWO) {
            //添加地址 --刷新
            onRefresh()

            return
        }
    }

    override fun binding(): ActivityAddressListBinding {
       return ActivityAddressListBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): AddressListAdapter {
        return AddressListAdapter(vm)
    }

    override fun title(): String? {
        return "地址管理"
    }
}
